<?php
require_once 'config/config_secu.php';
require_once 'config/config_db.php';



function getcompdatas($comp, &$compdatas) {
	$sql = "SELECT id_comp,computer,key_comp,mail_comp,receive_notice 
			FROM spy_computer
			WHERE `key_comp`='$comp'";
	$query = mysql_query($sql);
	if(mysql_num_rows($query)>0){
		$compdatas = mysql_fetch_array($query);
	}
	else {
		return "NO_COMPUTER";
	}
}

/*
 * les données à préparer sont :
 * computer : l'identifiant de l'ordinateur
 * id_address : l'adresse ip de la connexion
 * date : la date de cnx
 * time : l'heure de cnx
 */


/*
 * les infos de l'ordinateur qui se connecte
 */
$computer = (isset($_GET['ckey']))?htmlentities($_GET['ckey']):"";

if($computer == "" || empty($computer)){
	echo "error key".PHP_EOL;
	die;
}


$compdatas = '';

$error = getcompdatas($computer, $compdatas);


if($error != "" ||  !empty($error)){
	die;
}



/*
 * les infos de cnx
 *///print_r($_SERVER);
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1")
	$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
	$client_ip = $_SERVER['REMOTE_ADDR'];



/*
 * on régle la date
 */
$date = date("Y/m/d H:i:s");


/*
 * et on insére
 */
$sql = "INSERT INTO spy_receive (computer, ip_address, date, mail_sended, fid_comp)
		VALUES ('$computer', '$client_ip', '$date', 0, '{$compdatas['id_comp']}')";

mysql_query($sql);

$last_id = mysql_insert_id();

/*
 * envoi mail si besoin
 */
if($compdatas['receive_notice']==1 && !empty($compdatas['mail_comp'])){
	mail($compdatas['mail_comp'],
			"Notice from {$compdatas['computer']}",
			"Your computer {$compdatas['computer']} was seen at\n".
			"$date on ip address $client_ip");
	mysql_query("UPDATE spy_receive SET mail_sended=1");
}
